package ru.uskov.trainer.model.impl;

import java.math.BigDecimal;
import java.util.List;

import ru.uskov.trainer.model.IProduct;
import ru.uskov.trainer.model.IProductItem;

public class SummItem implements IProduct {

  private BigDecimal carbohydrates;
  private BigDecimal fat;
  private BigDecimal water;
  private BigDecimal proteins;
  private BigDecimal kkals;
  
  public SummItem(List<IProductItem> items) {
    BigDecimal carbohydrates = BigDecimal.ZERO;
    BigDecimal fat = BigDecimal.ZERO;
    BigDecimal water = BigDecimal.ZERO;
    BigDecimal proteins = BigDecimal.ZERO;
    BigDecimal kkals = BigDecimal.ZERO;
    synchronized (items) {
      for (IProductItem item : items) {
        carbohydrates = carbohydrates.add(item.getCarbohydrates());
        fat = fat.add(item.getFat());
        water = water.add(item.getWater());
        proteins = proteins.add(item.getProteins());
        kkals = kkals.add(item.getKKalls());
      } 
    }
    this.carbohydrates = carbohydrates.setScale(CARBOHYD_ACC);
    this.fat = fat.setScale(FAT_ACC);
    this.water = water.setScale(WATER_ACC);
    this.proteins = proteins.setScale(PROTEIN_ACC);
    this.kkals = kkals.setScale(KKAL_ACC);
  }
  
  @Override
  public BigDecimal getCarbohydrates() {
    return carbohydrates;
  }

  @Override
  public BigDecimal getProteins() {
    return proteins;
  }

  @Override
  public BigDecimal getFat() {
    return fat;
  }

  @Override
  public BigDecimal getKKalls() {
    return kkals;
  }

  @Override
  public BigDecimal getWater() {
    return water;
  }

  @Override
  public String getName() {
    return "Sum";
  }
}
